import { addApiInterceptor, removeApiInterceptor, type ApiInterceptor } from '../request'
import { usePageEffect, type PageEffectOptions } from './usePageEffect'

export function useApiInterceptor<ResponseDataType>(
  interceptor: ApiInterceptor<ResponseDataType>,
  options?: PageEffectOptions
) {
  return usePageEffect(
    {
      onInvoke: () => addApiInterceptor(interceptor),
      onUnload: () => removeApiInterceptor(interceptor),
    },
    options
  )
}
